package com.gxa.modules.service.impl;

import com.gxa.common.vo.JsonResult;
import com.gxa.modules.mapper.RoleMapper;
import com.gxa.modules.mapper.UserMapper;
import com.gxa.modules.pojo.R_Menu;
import com.gxa.modules.pojo.R_Role;
import com.gxa.modules.pojo.R_User;
import com.gxa.modules.pojo.Shop;
import com.gxa.modules.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wangyifan
 * @Date: 2022/01/05 10:36
 * @Description: $end$
 * @Version: V1.0
 */

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired(required = false)
    private RoleMapper roleMapper;

    @Autowired(required = false)
    private UserMapper userMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult returnMenu(String username) {
        JsonResult jsonResult = new JsonResult();

       R_User user = userMapper.findByUserName(username);
       if(user == null){
           return null;
       };
       List<R_Role> roleList = roleMapper.findRoleByUser(user.getUser_id());
        List<Long> listRoleId = new ArrayList<>();
        Boolean tamp = false;
        if(roleList != null){
            for (R_Role role:roleList) {
                listRoleId.add(role.getRole_id());
                if(role.getRolename().equals("超级管理员")){
                    tamp =true;
                };
            }
        }
        List<R_Menu> menuList = roleMapper.findMenuByRoleId(listRoleId);
        Map<String,List> map = new HashMap<>();
        map.put("menu",menuList);
        map.put("role",roleList);
        List<Shop> shopList = new ArrayList<>();
        if(tamp){
             shopList = userMapper.findShopByUser(null);
        }else{
             shopList = userMapper.findShopByUser(user.getUser_id());
        }
        map.put("shop",shopList);


        jsonResult.setData(map);
        jsonResult.setCode(200);
        jsonResult.setMsg("登录成功");
        return jsonResult;
    }
}
